package com.guage.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.guage.commons.result.PageInfo;
import com.guage.commons.result.Tree;
import com.guage.mapper.ShopGoodsCategoryMapper;
import com.guage.model.ShopGoodsCategory;
import com.guage.service.IShopGoodsCategoryService;

/**
 * <p>
 * 商品分类表 服务实现类
 * </p>
 *
 * @author dechao.yang
 * @since 2017-12-14
 */
@Service
public class ShopGoodsCategoryServiceImpl extends ServiceImpl<ShopGoodsCategoryMapper, ShopGoodsCategory> implements IShopGoodsCategoryService {
	 private static final int CATEGORY_PARENT_ID = 0; // 菜单
     @Autowired
     private ShopGoodsCategoryMapper shopGoodsCategoryMapper;
	
	@Override
	public void selectDataGrid(PageInfo pageInfo) {        
        Page<ShopGoodsCategory> page = new Page<ShopGoodsCategory>(pageInfo.getNowpage(), pageInfo.getSize());        
        EntityWrapper<ShopGoodsCategory> wrapper = new EntityWrapper<ShopGoodsCategory>();
        wrapper.orderBy(pageInfo.getSort(), pageInfo.getOrder().equalsIgnoreCase("ASC"));
        selectPage(page, wrapper);        
        pageInfo.setRows(page.getRecords());
        pageInfo.setTotal(page.getTotal());
	}
	 @Override
	public void selectPageDataByParams(PageInfo pageInfo) {
	        Page<Map<String, Object>> page = new Page<Map<String, Object>>(pageInfo.getNowpage(), pageInfo.getSize());
	        page.setOrderByField(pageInfo.getSort());
	        page.setAsc(pageInfo.getOrder().equalsIgnoreCase("asc"));
	        List<Map<String, Object>> list = shopGoodsCategoryMapper.selectPageDataByParams(page, pageInfo.getCondition());
	        pageInfo.setRows(list);
	        pageInfo.setTotal(page.getTotal());
	    }
	@Override
	public void insertByVo(ShopGoodsCategory shopGoodsCategoryVo) {
		shopGoodsCategoryMapper.insert(shopGoodsCategoryVo);
	}
	
	public List<ShopGoodsCategory> selectByType(Integer type) {
        EntityWrapper<ShopGoodsCategory> wrapper = new EntityWrapper<ShopGoodsCategory>();
        ShopGoodsCategory shopGoodsCategoryMapper = new ShopGoodsCategory();
        wrapper.setEntity(shopGoodsCategoryMapper);
      //  wrapper.addFilter("category_parent_id = {0}", type);
        wrapper.orderBy("category_sort");
        return shopGoodsCategoryMapper.selectList(wrapper);
    }
    
    @Override
    public List<Tree> selectAllMenu() {
        List<Tree> trees = new ArrayList<Tree>();
        // 查询所有菜单
        List<ShopGoodsCategory> shopGoodsCategorys = this.selectByType(CATEGORY_PARENT_ID);
        if (shopGoodsCategorys == null) {
            return trees;
        }
        for (ShopGoodsCategory shopGoodsCategory : shopGoodsCategorys) {
            Tree tree = new Tree();
            tree.setId(shopGoodsCategory.getCategoryId());
            tree.setPid(shopGoodsCategory.getCategoryParentId());
            tree.setText(shopGoodsCategory.getCategoryName());
            tree.setIconCls(shopGoodsCategory.getCategoryImage());
           // tree.setAttributes(shopGoodsCategory.getUrl());
           // tree.setState(shopGoodsCategory.getOpened());
            trees.add(tree);
        }
        return trees;
    }
}
